# coding: utf-8
import os

class Scheduled_Tasks():
    def __init__(self):
        self.comm_path = os.path.dirname(os.path.abspath(__file__))
        self.log_path = self.comm_path + "/crontab.log"
    def Set_restart_task(self, commands,comments):
        '''
        生成重启后一分钟执行的任务
        :return:
        '''
        os.system(f"crontab -l > {self.log_path}")
        with open(self.log_path,"a") as f:
            f.write(f"@reboot {commands} # {comments}\n")
            f.close()
        os.system(f"crontab {self.log_path}")
        os.system(f"rm {self.log_path}")


    def check_function(self,commands):
        os.system(f"crontab -l > {self.log_path}")
        with open(self.log_path, "r") as f:
            text = f.read()
            if commands in text:
                a = 1
            else:
                a = 0
            f.close()
        os.system(f"rm {self.log_path}")
        return a
    def delet_task(self):
        os.system("crontab -r")
